package com.woniuxy.joylifeshop.service.serviceImpl;

import com.woniuxy.common.entity.ShopAddress;
import com.woniuxy.common.entity.ShopUserAddress;
import com.woniuxy.common.entity.ShopUserAddressExample;
import com.woniuxy.common.mapper.ShopAddressMapper;
import com.woniuxy.common.mapper.ShopUserAddressMapper;
import com.woniuxy.joylifeshop.exception.AddressException;
import com.woniuxy.joylifeshop.exception.ShopUserAddressException;
import com.woniuxy.joylifeshop.service.ShopUserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class ShopUserAddressServiceImpl implements ShopUserAddressService {

    @Autowired
    ShopUserAddressMapper shopUserAddressMapper;

    @Autowired
    ShopAddressMapper shopAddressMapper;
    /**
    * @Author SooYoo
    * @Description
    * @Param
    * @Return
    **/
    @Override
    public ShopUserAddress addShopUserAddress(String name, String phone,String userId,String addrId) {


        try {
            ShopUserAddress shopUserAddress = new ShopUserAddress();
            shopUserAddress.setId(UUID.randomUUID().toString());
            shopUserAddress.setName(name);
            shopUserAddress.setUserId(userId);
            shopUserAddress.setPhone(phone);
            shopUserAddress.setPriority("非默认");
            shopUserAddress.setAddressId(addrId);

            shopUserAddressMapper.insert(shopUserAddress);

            return shopUserAddress;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ShopUserAddressException();
        }

    }

    /**
    * @Author SooYoo
    * @Description 新建一个地址对象
    * @Param
    * @Return
    **/
    @Override
    public ShopAddress addShopAddress(String country, String province, String city, String detail) {


        try {
            ShopAddress shopAddress = new ShopAddress();
            shopAddress.setAddrId(UUID.randomUUID().toString());
            shopAddress.setCountry(country);
            shopAddress.setProvince(province);
            shopAddress.setCity(city);
            shopAddress.setDetail(detail);


            shopAddressMapper.insert(shopAddress);

            return shopAddress;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ShopUserAddressException();
        }

    }

    /**
    * @Author SooYoo
    * @Description 删除一条地址
    * @Param
    * @Return
    **/
    @Override
    public void deleteAddressAndUserAddress(String addrId) {

        try {
            //删除userAddr
            ShopUserAddressExample shopUserAddressExample = new ShopUserAddressExample();
            ShopUserAddressExample.Criteria criteria = shopUserAddressExample.createCriteria();
            criteria.andAddressIdEqualTo(addrId);
            shopUserAddressMapper.deleteByExample(shopUserAddressExample);
            //删除addr
            shopAddressMapper.deleteByPrimaryKey(addrId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AddressException();
        }


    }
}
